package com.gyc.simple.sysuser.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyc.simple.common.util.UUIDUtils;
import com.gyc.simple.sysuser.dao.base.ISourceMapper;
import com.gyc.simple.sysuser.entity.base.ISource;
import com.gyc.simple.sysuser.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class SysResourceServiceImpl implements SysResourceService {
    @Autowired
    private ISourceMapper iSourceMapper;


    @Override
    public PageInfo queryListByPageCondition(Map paramap) {
        PageHelper.startPage(Integer.parseInt((String) paramap.get("pageNum")),Integer.parseInt((String)paramap.get("pageSize")));
        return new PageInfo(iSourceMapper.queryListByPageCondition(paramap));
    }

    @Override
    public List<ISource> queryAll() {
        return iSourceMapper.queryAll();
    }

    @Override
    public boolean SourceInit(Map<String, String> map) {
        List<String> old = iSourceMapper.sourceUrl();
        for (int i=0;i<old.size();i++){
            if(map.containsKey(old.get(i))){
                map.remove(old.get(i));
            }
        }
        List<ISource> iSources = new ArrayList<>();
        for (Map.Entry<String,String> keyValue : map.entrySet()){
            ISource iSource = new ISource();
            iSource.setIsourcecid(UUIDUtils.getUUID32());
            iSource.setIsourceurl(keyValue.getKey());
            iSource.setIdesc(keyValue.getValue());

            iSource.setIsourceorg("sys");
            iSource.setIsourcestatus(1);
            iSource.setIsourcetype(0);
            iSources.add(iSource);
        }
        if(iSources.size()>0){
            if(iSourceMapper.sourceInit(iSources)>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<ISource> selectByUID(String uid) {
        return iSourceMapper.selectByUID(uid);
    }
}
